import cv2
import sys
import os
from PyQt5.QtWidgets import QMainWindow, QFileDialog, QApplication, QWidget
from PyQt5.QtCore import QThread, pyqtSignal
from PyQt5.QtCore import Qt

from ui.ui.video_ui2 import *
from ui.ui.ui_base import ui_base


class video_ui(QWidget, Ui_Form, ui_base):
    slidervalue = 0
    sliderflag = False
    width = 0
    height = 0
    last_x = -1
    last_y = -1
    is_move = False
    mousebutton = 0
    zoomode = 0
    playspeed = 0
    frame_num = 0
    frame_id = 0

    def __init__(self, admin, parent=None):
        super(video_ui, self).__init__(parent)
        self.admin = admin
        self.setupUi(self)
        self.set_activity()

    def set_activity(self):
        self.admin.video_therad.set_label(self.label)
        self.label.setMouseTracking(True)
        self.playButton.clicked.connect(self.paly_control)
        self.addButton.clicked.connect(self.add_control)
        self.subButton.clicked.connect(self.sub_control)
        self.lastButton.clicked.connect(self.last_control)
        self.nextButton.clicked.connect(self.next_control)
        self.lineEdit.editingFinished.connect(self.editingFinished)
        self.setMouseTracking(True)
        self.horizontalSlider.valueChanged.connect(self.valueChanged)
        self.horizontalSlider.sliderReleased.connect(self.sliderReleased)
        # self.horizontalSlider.setTickInterval(1)
        # self.horizontalSlider.setMaximum(1000)

    def valueChanged(self, value):
        self.sliderflag = True
        self.slidervalue = value
        self.lineEdit.setText(str(value) + '/' + str(self.frame_num))

    def sliderReleased(self):
        # print(self.slidervalue)
        self.admin.video_therad.set_frame_id(self.slidervalue)
        self.sliderflag = False

    def set_size(self, w, h):
        if w < 640:
            w = 640
        if h < 480:
            h = 480
        self.width = w
        self.height = h
        self.resize(w, h + 70)
        self.label.resize(w, h)
        self.horizontalSlider.setGeometry(0, h, w, 30)
        self.playButton.setGeometry(10, h + 30, 50, 30)
        self.addButton.setGeometry(70, h + 30, 50, 30)
        self.subButton.setGeometry(130, h + 30, 50, 30)
        self.lastButton.setGeometry(190, h + 30, 50, 30)
        self.nextButton.setGeometry(250, h + 30, 50, 30)
        self.label_3.setGeometry(310, h + 30, 85, 30)
        self.label_2.setGeometry(395, h + 30, 85, 30)
        self.lineEdit.setGeometry(w - 150, h + 30, 140, 30)
        self.label_3.setText('快进x' + str(self.playspeed))

    def paly_control(self):
        if self.playButton.text() == '播放':
            self.admin.video_therad.set_pause(True)
            self.playButton.setText('暂停')
        else:
            self.admin.video_therad.set_pause(False)
            self.playButton.setText('播放')

    def add_control(self):
        ret = self.admin.video_therad.set_fps(1.25)
        if ret is True:
            self.playspeed = self.playspeed + 0.5
            self.label_3.setText('快进x' + str(self.playspeed))

    def sub_control(self):
        ret = self.admin.video_therad.set_fps(0.75)
        if ret is True:
            self.playspeed = self.playspeed - 0.5
            self.label_3.setText('快进x' + str(self.playspeed))

    def last_control(self):
        self.admin.video_therad.last_frame()

    def next_control(self):
        self.admin.video_therad.next_frame()

    def editingFinished(self):
        txt = self.lineEdit.text().split('/')
        print(txt[0])

    def set_frame_id(self, id):
        self.frame_id = int(id)
        if self.sliderflag is True:
            return
        if self.frame_id < 0:
            self.lineEdit.setText(str(0) + '/' + str(0))
            self.horizontalSlider.setValue(0)
        else:
            self.lineEdit.setText(str(self.frame_id) + '/' + str(self.frame_num))
            self.horizontalSlider.setValue(self.frame_id)

    def set_frame_num(self, num):
        self.frame_num = int(num)
        if self.frame_num < 0:
            self.horizontalSlider.setMaximum(0)
        else:
            self.horizontalSlider.setMaximum(self.frame_num)

    # 鼠标移动
    def mouseMoveEvent(self, event):
        x = event.x()
        y = event.y()
        if x < self.width and y < self.height:
            self.admin.video_therad.set_linecoor([x, y])
            if self.mousebutton == Qt.LeftButton:
                if self.is_move:
                    self.admin.video_therad.move_nowRect((x - self.last_x), (y - self.last_y))
                    self.last_x = x
                    self.last_y = y
                else:
                    self.admin.video_therad.set_nowRect([self.last_x, self.last_y, x, y])

    # 鼠标按下
    def mousePressEvent(self, event):
        self.mousebutton = event.button()
        x = event.x()
        y = event.y()
        if x < self.width and y < self.height:
            self.last_x = x
            self.last_y = y
            if event.button() == Qt.LeftButton:
                self.is_move = self.admin.video_therad.get_nowRect(x, y)

    # 鼠标释放
    def mouseReleaseEvent(self, event):
        self.mousebutton = 0
        x = event.x()
        y = event.y()
        if x < self.width and y < self.height:
            if event.button() == Qt.LeftButton:
                if (self.last_x - x) ** 2 <= 3 ** 2 or (self.last_y - y) ** 2 <= 3 ** 2:
                    self.admin.video_therad.get_nowRect(x, y)
                else:
                    self.admin.video_therad.addRect(self.last_x, self.last_y, x, y)
            elif event.button() == Qt.RightButton:
                self.admin.video_therad.get_nowRect(x, y)
                self.admin.video_therad.delRect()

    # 双击鼠标
    def mouseDoubleClickEvent(self, event):
        if event.button() == Qt.LeftButton:
            self.zoomode = (self.zoomode + 1) % 3
            if self.zoomode == 0:
                self.label_2.setText('缩放模式:xy')
            elif self.zoomode == 1:
                self.label_2.setText('缩放模式:x')
            elif self.zoomode == 2:
                self.label_2.setText('缩放模式:y')

    # 滚轮滑动
    def wheelEvent(self, event):
        angle = event.angleDelta().y() // 120
        self.admin.video_therad.resize_nowRect(angle, self.zoomode)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    win = video_ui()
    win.show()
    app.exec_()
